package com.kehutong.webchat.util;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.coraframework.http.HttpClient;
import org.coraframework.inject.Inject;
import org.coraframework.inject.Singleton;
import org.coraframework.json.JSON;
import org.coraframework.json.JSONObject;
import org.coraframework.util.CallBack;
import org.coraframework.util.Objects;

import com.kehutong.common.DoveClient;
import com.kehutong.common.urlconfig.UrlConfig;
import com.kehutong.webchat.entity.WeChatSetting;
import com.kehutong.webchat.service.WeChatService;

@Singleton
public class WechatHttpClient extends UrlConfig {
	
	private static final Set<Integer> ILL_TOKEN_SET;
	
	static {
		Set<Integer> map = new HashSet<>();
		map.add(40014);
		map.add(42001);
		ILL_TOKEN_SET = Collections.unmodifiableSet(map);
	}
	
	protected WechatHttpClient() {
		super("tencent", "webchat_url");
	}


	@Inject
	private DoveClient doveClient;
	@Inject
	private WeChatService wechatService;
	
	@Override
	public JSONObject get(String page, CallBack<HttpClient> callBack) throws Exception {
		return super.get(page, callBack);
	}
	
	@Override
	public JSONObject post(String page, CallBack<HttpClient> client) throws Exception {
		return super.post(page, client);
	}

	public byte[] getPostToImage(String page, CallBack<HttpClient> client) throws Exception{
		final HttpClient httpClient = HttpClient.create(getUrl() + page);
		client.call(httpClient);
		return httpClient.download();
	}


	// 使用企业自身access_token进行请求
	public JSONObject postByAccessToken(String page, WeChatSetting setting, CallBack<HttpClient> callBack) throws Exception {
		final HttpClient httpClient = HttpClient.create(getUrl() + page);
		httpClient.addParam("access_token", wechatService.getAccessToken(setting));
		callBack.call(httpClient);

		String text = httpClient.post();
		if (httpClient.getResponseCode() != 200) {
			return httpError(httpClient.getResponseCode(), "http状态错误");
		}
		
		JSONObject rs = JSON.parseObject(text);
		checkIllnessAccessToken(setting, rs.getIntValue("errcode"));
		return rs;
	}
	
	public JSONObject getByAccessToken(String page, WeChatSetting setting, CallBack<HttpClient> callBack) throws Exception {
		final HttpClient httpClient = HttpClient.create(getUrl() + page);
		httpClient.addParam("access_token", wechatService.getAccessToken(setting));
		callBack.call(httpClient);

		String text = httpClient.get();
		if (httpClient.getResponseCode() != 200) {
			return httpError(httpClient.getResponseCode(), "http状态错误");
		}
		
		JSONObject rs = JSON.parseObject(text);
		checkIllnessAccessToken(setting, rs.getIntValue("errcode"));
		return rs;
	}
	
	public JSONObject postKht(String page, String corpNo, CallBack<HttpClient> call) throws Exception {
		return doveClient.post(page, http-> {
			if (Objects.nonNull(corpNo)) {
				http.addHeader("companyNo", corpNo);
			}
			call.call(http);
		});
	}
	
	private void checkIllnessAccessToken(WeChatSetting setting, int errcode) throws Exception {
		if (!ILL_TOKEN_SET.contains(errcode)) {
			return;
		}
		
		setting.setAccessToken(null);
		setting.setAccessTokenExpiredTime(null);
		wechatService.getAccessToken(setting);
	}
	
}
